package com.test.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class LockApp {

	public static void main(String[] args) {
		final ReentrantLock lock = new ReentrantLock();
		final Condition condition = lock.newCondition();
		for(int i=0;i<10;i++) {
			new Thread() {
				public void run() {
					try {
						lock.lock();
						System.out.println("加锁");
						Thread.sleep(1000*5);
						System.out.println("锁住5秒");
						condition.await();
//						count++;
						System.out.println(Thread.currentThread().getName());
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}finally {
						lock.unlock();
					}
				};
			}.start();
		}
		try {
			lock.lock();
			System.out.println("游戏开始");
			for(int i=0; i<10; i++) {
				Thread.sleep(1 * 1000);
				System.out.println("唤醒");
				condition.signal();
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			lock.unlock();
		}

	}

}
